﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Text.RegularExpressions;
using FrbaBus.Core;

namespace FrbaBus.Abm_Micro
{
    public partial class ModificarMicro : Form
    {
        public ModificarMicro()
        {
            InitializeComponent();
        }

        private ABMMicro parent = null;
        public ModificarMicro (Form callingForm)
        {
            parent = callingForm as ABMMicro;

            InitializeComponent();
        }



        public string query = null;
        public bool first;
        public DataGridViewRow Row;
        
        

        private void addpathern()
        {
            if (first == true)
            {
                first = false;
                query = query + " where";
            }
            else
                query = query + " AND";
        }



        private void button3_Click(object sender, EventArgs e)
        {
            query = " from ALL_IN.MicrosMarca";
            dataGridView1.Rows.Clear();
            DBManager db = new DBManager();
            first = true;
            if (textMicro.Text != "")
            {
                addpathern();
                DateTime ActualDate = Convert.ToDateTime(System.Configuration.ConfigurationManager.AppSettings["Fecha"]);
                string ActualHora;
                ActualHora = ActualDate.ToString("yyyy-MM-dd");
                ActualHora = ActualHora + " 23:59:00.000";
                query = query + " Fecha_Alta < '" + ActualHora + "' ";
                addpathern();
                query = query + " Patente like '%" + textMicro.Text + "%' OR Fecha_Alta like '%" + textMicro.Text + "%' OR Nro_Micro like '%" + textMicro.Text + "%' OR Modelo like '%" + textMicro.Text
                    + "%' OR Marca like '%" + textMicro.Text + "%' OR Tipo_Servicio like '%" + textMicro.Text + "%' OR Baja_Fuera_de_Servicio like '%" + textMicro.Text + "%' OR Baja_Vida_Util like '%" + textMicro.Text
                    + "%' OR Cantidad_Kg like '%" + textMicro.Text + "%' OR Fecha_Fuera_de_Servicio like '%" + textMicro.Text + "%' OR Fecha_Reinicio_Servicio like '%" + textMicro.Text 
                    + "%' OR Fecha_Baja_Definitiva like '%" + textMicro.Text + "%'";
                 
            }
            if ( parent.modificacion == false )
            {
                addpathern();
                query = query + " Baja_Vida_Util = 0";
                addpathern();
                query = query + " Baja_Fuera_de_Servicio = 0";
            }
            SqlCommand consulta = new SqlCommand("select *" + query);
            SQLResponse r = db.executeQuery(consulta);
            SqlCommand consulta2 = new SqlCommand("select count (*)" + query);
            SQLResponse r2 = db.executeQuery(consulta2);
            int valor = (int)r2.result.Rows[0][0];
            for (int i = 0; i < valor; i++)
            {
                dataGridView1.Rows.Add(r.result.Rows[i][0].ToString(), r.result.Rows[i][1].ToString(), r.result.Rows[i][2].ToString(), r.result.Rows[i][3].ToString(), r.result.Rows[i][4].ToString(), r.result.Rows[i][5].ToString(), r.result.Rows[i][6].ToString(), r.result.Rows[i][7].ToString(), r.result.Rows[i][8].ToString(), r.result.Rows[i][9].ToString(), r.result.Rows[i][10].ToString(), r.result.Rows[i][11].ToString());
            }
 
        }

        private void ModificarMicro_Load(object sender, EventArgs e)
        {
            dataGridView1.ReadOnly = true;
            dataGridView1.AllowUserToAddRows = false;
            dataGridView1.AllowUserToDeleteRows = false;
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void button4_Click(object sender, EventArgs e)
        {
            dataGridView1.Rows.Clear();
            textMicro.Visible = false;
            label1.Visible = true; label2.Visible = true; label3.Visible = true; label4.Visible = true; label6.Visible = true;
            textBox1.Visible = true; textBox2.Visible = true;
            comboBox1.Visible = true; comboBox2.Visible = true; comboBox3.Visible = true; comboBox4.Visible = true;
            button3.Visible = false; button4.Visible = false; button6.Visible = false; button7.Visible = true;
            button5.Visible = true;
            domainUpDown1.Visible = true;
            domainUpDown1.ReadOnly = true;
            checkBox1.Visible = true;
            checkBox2.Visible = true;

            DBManager db = new DBManager();
            SqlCommand consulta = new SqlCommand("SELECT Nro_Micro  from ALL_IN.MicrosMarca order by 1");
            SQLResponse r = db.executeQuery(consulta);
            SqlCommand consulta1 = new SqlCommand("select COUNT (distinct (Nro_Micro) ) from ALL_IN.MicrosMarca");
            SQLResponse r1 = db.executeQuery(consulta1);
            int valor = (int)r1.result.Rows[0][0];
            for (int i = 0; i < valor; i++)
            {
                comboBox2.Items.Add(r.result.Rows[i][0].ToString());
            }
            SqlCommand consulta2 = new SqlCommand("SELECT distinct (Modelo)  from ALL_IN.MicrosMarca order by 1");
            SQLResponse r2 = db.executeQuery(consulta2);
            SqlCommand consulta3 = new SqlCommand("select COUNT (distinct (Modelo) ) from ALL_IN.MicrosMarca");
            SQLResponse r3 = db.executeQuery(consulta3);
            valor = (int)r3.result.Rows[0][0];
            for (int i = 0; i < valor; i++)
            {
                comboBox3.Items.Add(r2.result.Rows[i][0].ToString());
            }

            SqlCommand consulta4 = new SqlCommand("SELECT distinct (Marca)  from ALL_IN.MicrosMarca order by 1");
            SQLResponse r4 = db.executeQuery(consulta4);
            SqlCommand consulta5 = new SqlCommand("select COUNT (distinct (Marca) ) from ALL_IN.MicrosMarca");
            SQLResponse r5 = db.executeQuery(consulta5);
            valor = (int)r5.result.Rows[0][0];
            for (int i = 0; i < valor; i++)
            {
                comboBox4.Items.Add(r4.result.Rows[i][0].ToString());
            }

            SqlCommand consulta6 = new SqlCommand("SELECT distinct (Tipo_Servicio)  from ALL_IN.Tipo_Servicio order by 1");
            SQLResponse r6 = db.executeQuery(consulta6);
            SqlCommand consulta7 = new SqlCommand("select COUNT (distinct (Tipo_Servicio) ) from ALL_IN.Tipo_Servicio");
            SQLResponse r7 = db.executeQuery(consulta7);
            valor = (int)r7.result.Rows[0][0];
            for (int i = 0; i < valor; i++)
            {
                comboBox1.Items.Add(r6.result.Rows[i][0].ToString());
            }

            db.desconectar();
            
        }

        private void button5_Click(object sender, EventArgs e)
        {
            monthCalendar1.Visible = true;
            textBox2.Visible = false;
            button5.Visible = false;
            button6.Visible = true;
            domainUpDown1.Enabled = true;
                    }

        private void monthCalendar1_DateChanged(object sender, DateRangeEventArgs e)
        {
            DateTime dia = monthCalendar1.SelectionStart;
            string registro = dia.ToString("dd/MM/yyyy");
            textBox2.Text = registro;
            monthCalendar1.Visible = true;
            button5.Visible = false;
            button6.Visible = true;

        }

        private void button6_Click(object sender, EventArgs e)
        {
            button6.Visible = false;
            button5.Visible = true;
            monthCalendar1.Visible = false;
            textBox2.Visible = true;
            domainUpDown1.Enabled = false;
        }

        private void button7_Click(object sender, EventArgs e)
        {
            query = " from ALL_IN.MicrosMarca";
            DBManager db = new DBManager();
            first = true;
            dataGridView1.Rows.Clear();
            if (textBox1.Text != "")
            {
                this.addpathern();
                query = query + " Patente like '%" + textBox1.Text + "%'";
            }
            if (comboBox2.Text != "")
            {
                addpathern();
                query = query + " Nro_Micro='" + comboBox2.Text + "'";
            }
            if (comboBox1.Text != "")
            {
                addpathern();
                query = query + " Tipo_Servicio='" + comboBox1.Text + "'";
            }
            if (comboBox3.Text != "")
            {
                addpathern();
                query = query + " Modelo='" + comboBox3.Text + "'";
            }
            if (comboBox4.Text != "")
            {
                addpathern();
                query = query + " Marca='" + comboBox4.Text + "'";
            }
            if (textBox2.Text != "")
            {
                this.addpathern();
                query = query + " Fecha_Alta like '%" + textBox2.Text + "%'";
            }
            if (checkBox1.Checked == true)
            {
                this.addpathern();
                query = query + " Baja_Fuera_de_Servicio='1'";
            }
            if (checkBox2.Checked == true)
            {
                this.addpathern();
                query = query + " Baja_Vida_Util='1'";
            }
             

            SqlCommand consulta = new SqlCommand("select *" + query);
            SQLResponse r = db.executeQuery(consulta);
            SqlCommand consulta2 = new SqlCommand("select count (*)" + query);
            SQLResponse r2 = db.executeQuery(consulta2);
            int valor = (int)r2.result.Rows[0][0];
            for (int i = 0; i < valor; i++)
            {
                dataGridView1.Rows.Add(r.result.Rows[i][0].ToString(), r.result.Rows[i][1].ToString(), r.result.Rows[i][2].ToString(), r.result.Rows[i][3].ToString(), r.result.Rows[i][4].ToString(), r.result.Rows[i][5].ToString(), r.result.Rows[i][6].ToString(), r.result.Rows[i][7].ToString(), r.result.Rows[i][8].ToString(), r.result.Rows[i][9].ToString(), r.result.Rows[i][10].ToString(), r.result.Rows[i][11].ToString());
            }



        }
        public string OldPatente = null;
        private void button2_Click(object sender, EventArgs e)
        {



            if (dataGridView1.SelectedRows.Count != 0)
            {
                Row = dataGridView1.SelectedRows[0];
                OldPatente = String.Copy(this.Row.Cells[0].Value.ToString());


                if (parent.modificacion == true)
                {
                    Abm_Micro.ModificarMicro2 frm = new Abm_Micro.ModificarMicro2(this);
                    frm.ShowDialog(this);
                }
                else
                {
                    // SE HIZO CLICK EN BORRAR MICRO
                    DBManager db = new DBManager();
                    Abm_Micro.BajaMicro frm = new Abm_Micro.BajaMicro(this);
                    frm.ShowDialog(this);
                }
                this.Close();
            }
            
        }

        private void button1_Click(object sender, EventArgs e)
        {
            dataGridView1.Rows.Clear();
        }


        }

        }

  

        
    

